<?php

/**
 * This is the model class for table "{{employee}}".
 *
 * The followings are the available columns in table '{{employee}}':
 * @property integer $id
 * @property integer $userId
 * @property string $familyName
 * @property string $givenName
 * @property integer $employed
 * @property string $documentNumber
 * @property integer $gender
 * @property string $ethnicGroup
 * @property string $homeTown
 * @property string $education
 * @property string $profession
 * @property string $dateOfBirth
 * @property string $healthCondition
 * @property string $hireDate
 * @property string $maritalStatus
 * @property string $currentRank
 * @property string $politicalStatus
 * @property string $contactMobile
 * @property string $contactPhone
 * @property string $originalHomeAddress
 * @property string $currentAddress
 * @property string $emergencyContactName
 * @property string $emergencyContactPhone
 * @property string $identityCardNumber
 * @property string $graduationSchool
 * @property string $certificates
 * @property string $personalHabits
 * @property integer $personalSocialPension
 * @property integer $personalSocialInjury
 * @property integer $personalSocialUemployed
 * @property integer $personalSocialIllness
 * @property integer $personalSocialInsurance
 * @property string $familyMembers
 * @property string $educationDetails
 * @property string $workExperience
 * @property string $outstandingAchievement
 * @property string $note
 * @property string $trialDate
 * @property string $trialSalary
 * @property string $employedDate
 * @property string $unemployedDate
 * @property integer $signedCommitment
 * @property integer $signedSecrecyAgreement
 * @property integer $signedTrailContract
 * @property integer $signedEmploymentContract
 * @property integer $signedGuaranteeStatement
 * @property integer $signedJobStatement
 * @property string $companyNote
 * @property string $personalPhotoFile
 * @property integer $currentProjectId
 * @property string $createTime
 * @property integer $createUserId
 * @property string $updateTime
 * @property integer $updateUserId
 *
 * The followings are the available model relations:
 * @property User $updateUser
 * @property User $createUser
 * @property User $user
 * @property Project $currentProject
 */

class Employee extends CActiveRecord {
	/**
	 * Returns the static model of the specified AR class.
	 * @return Employee the static model class
	 */

	public static function model($className = __CLASS__) {
		return parent::model($className);
	}
	
	public static function getAllStatus(){
		return array(
			1=>'正式',
			2=>'实习',
			3=>'试用',
			4=>'借调',
			5=>'挂靠',
			6=>'离职',
		);
	}

	/**
	 * 
	 * @return Employee[] 
	 */
	public static function findAllByTag($key, $includeId = null) {
		$model = self::model();
		return $model->cache(20)
			->findAll(
				array(
					'condition' => $includeId ? '( ( tags.`key`=:key AND '
							. $model->getTableAlias() . '.employed=1) OR '
							. $model->getTableAlias() . '.id=:employeeId )'
						: 'tags.`key`=:key AND ' . $model->getTableAlias() . '.employed=1',
					'params' => $includeId ? array(
							':key' => $key,
							':employeeId' => $includeId
						) : array(
							':key' => $key,
						),
					'with' => 'tags',
					'together' => true
				));
	}

	/**
	 * @return string the associated database table name
	 */

	public function tableName() {
		return '{{employee}}';
	}

	public function scopes() {
		return array(
			'employed' => array(
				'condition' => $this->getTableAlias() . '.employed=1'
			),
		);
	}

	/**
	 * @return array validation rules for model attributes.
	 */

	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'familyName, givenName, gender',
				'required'
			),
			array(
				'userId, employed, gender, personalSocialPension, 
			personalSocialInjury, personalSocialUemployed, personalSocialIllness, 
			personalSocialInsurance, signedCommitment, signedSecrecyAgreement, 
			signedTrailContract, signedEmploymentContract, signedGuaranteeStatement, 
			signedJobStatement, status, currentProjectId',
				'numerical',
				'integerOnly' => true
			),
			array(
				'familyName, givenName, documentNumber, ethnicGroup, homeTown, 
				education, profession, healthCondition, maritalStatus, currentRank, 
				politicalStatus, contactMobile, contactPhone, originalHomeAddress, 
				currentAddress, emergencyContactName, emergencyContactPhone, 
				identityCardNumber, graduationSchool, certificates, personalHabits',
				'length',
				'max' => 255
			),
			array(
				'personalPhotoFile',
				'file',
				'types' => 'jpg, gif, png',
				'allowEmpty' => true,
			),
			array(
				'personalPhotoFile',
				'unsafe'
			),
			array(
				'trialSalary',
				'length',
				'max' => 8
			),
			array(
				'trialSalary',
				'numerical',
			),
			array(
				'trialSalary, dateOfBirth, hireDate, trialDate, employedDate, unemployedDate',
				'default',
				'value' => null
			),
			array(
				'dateOfBirth, hireDate, trialDate, employedDate, unemployedDate',
				'date',
				'format' => 'yyyy-MM-dd',
			),
			array(
				'familyMembers, educationDetails, workExperience, 
				outstandingAchievement, note, companyNote',
				'safe'
			),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array(
				'id, userId, familyName, givenName, employed, documentNumber, gender, ethnicGroup, homeTown, education, profession, dateOfBirth, healthCondition, hireDate, maritalStatus, currentRank, politicalStatus, contactMobile, contactPhone, originalHomeAddress, currentAddress, emergencyContactName, emergencyContactPhone, identityCardNumber, graduationSchool, certificates, personalHabits, personalSocialPension, personalSocialInjury, personalSocialUemployed, personalSocialIllness, personalSocialInsurance, familyMembers, educationDetails, workExperience, outstandingAchievement, note, trialDate, trialSalary, employedDate, unemployedDate, signedCommitment, signedSecrecyAgreement, signedTrailContract, signedEmploymentContract, signedGuaranteeStatement, signedJobStatement, companyNote, personalPhotoFile, createTime, createUserId, updateTime, updateUserId',
				'safe',
				'on' => 'search'
			),
		);
	}

	public function behaviors() {
		return array(
			'AuditableBehavior' => array(
				'class' => 'components.db.AuditableBehavior',
				'attributes' => 'familyName, givenName, employed, documentNumber, gender, ethnicGroup, homeTown, education, profession, dateOfBirth, healthCondition, hireDate, maritalStatus, currentRank, politicalStatus, contactMobile, contactPhone, originalHomeAddress, currentAddress, emergencyContactName, emergencyContactPhone, identityCardNumber, graduationSchool, certificates, personalHabits, personalSocialPension, personalSocialInjury, personalSocialUemployed, personalSocialIllness, personalSocialInsurance, familyMembers, educationDetails, workExperience, outstandingAchievement, note, trialDate, trialSalary, employedDate, unemployedDate, signedCommitment, signedSecrecyAgreement, signedTrailContract, signedEmploymentContract, signedGuaranteeStatement, signedJobStatement, companyNote, personalPhotoFile,
				currentProjectId'
			),
			'AuditStampBehavior' => array(
				'class' => 'components.db.AuditStampBehavior',
			),
			'PersonalPhotoUploadBehavior' => array(
				'class' => 'components.db.ProcessUploadImageBehavior',
				'attribute' => 'personalPhotoFile',
				'imageWidth' => 400,
				'imageHeight' => 560,
				'imageFolder' => 'employeePhoto',
				'thumbAttribute' => 'personalPhotoThumb',
				'thumbWidth' => 100,
				'thumbHeight' => 140,
			),
		);
	}

	/**
	 * @return array relational rules.
	 */

	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'updateUser' => array(
				self::BELONGS_TO,
				'User',
				'updateUserId'
			),
			'createUser' => array(
				self::BELONGS_TO,
				'User',
				'createUserId'
			),
			'currentProject' => array(
				self::BELONGS_TO,
				'Project',
				'currentProjectId'
			),
			'user' => array(
				self::BELONGS_TO,
				'User',
				'userId'
			),
			'tags' => array(
				self::MANY_MANY,
				'EmployeeTag',
				'{{employee_tag_employee}}(employeeId, tagId)'
			),
			'certificateItems' => array(
				self::HAS_MANY,
				'EmployeeCertificate',
				'employeeId'
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */

	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'userId' => '登陆用户',
			'familyName' => '姓',
			'givenName' => '名',
			'employed' => '在职',
			'documentNumber' => '档案编号',
			'gender' => '性别',
			'ethnicGroup' => '民族',
			'homeTown' => '籍贯',
			'education' => '学历',
			'profession' => '专业',
			'dateOfBirth' => '出生日期',
			'healthCondition' => '健康状况',
			'hireDate' => '入司时间',
			'maritalStatus' => '婚姻状况',
			'currentRank' => '当前职级',
			'politicalStatus' => '政治面貌',
			'contactMobile' => '联系电话',
			'contactPhone' => '家庭电话',
			'originalHomeAddress' => '原籍住址',
			'currentAddress' => '现住址',
			'emergencyContactName' => '紧急联系人',
			'emergencyContactPhone' => '紧急联系电话',
			'identityCardNumber' => '身份证号',
			'graduationSchool' => '毕业学校',
			'certificates' => '所获证书',
			'personalHabits' => '爱好特长',
			'personalSocialPension' => '养老保险',
			'personalSocialInjury' => '工伤保险',
			'personalSocialUemployed' => '失业保险',
			'personalSocialIllness' => '大病保险',
			'personalSocialInsurance' => '社保',
			'familyMembers' => '家庭成员',
			'educationDetails' => '教育经历',
			'workExperience' => '工作经历',
			'outstandingAchievement' => '主要业绩',
			'note' => '个人备注',
			'trialDate' => '试用期日期',
			'trialSalary' => '试用期工资',
			'employedDate' => '正式聘用日期',
			'unemployedDate' => '离职日期',
			'signedCommitment' => '已签入职承诺书',
			'signedSecrecyAgreement' => '已签保密协议',
			'signedTrailContract' => '已签试用合同',
			'signedEmploymentContract' => '已签聘用合同',
			'signedGuaranteeStatement' => '已签担保书',
			'signedJobStatement' => '已签岗位职责',
			'companyNote' => '公司备注',
			'personalPhotoFile' => '个人照片',
			'currentProjectId' => '当前项目',
			'createTime' => '创建时间',
			'createUserId' => '创建用户',
			'updateTime' => '更新时间',
			'updateUserId' => '更新用户',
			'status' => '状态'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */

	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('userId', $this->userId);
		$criteria->compare('familyName', $this->familyName, true);
		$criteria->compare('givenName', $this->givenName, true);
		$criteria->compare('employed', $this->employed);
		$criteria->compare('documentNumber', $this->documentNumber, true);
		$criteria->compare('gender', $this->gender);
		$criteria->compare('ethnicGroup', $this->ethnicGroup, true);
		$criteria->compare('homeTown', $this->homeTown, true);
		$criteria->compare('education', $this->education, true);
		$criteria->compare('profession', $this->profession, true);
		$criteria->compare('dateOfBirth', $this->dateOfBirth, true);
		$criteria->compare('healthCondition', $this->healthCondition, true);
		$criteria->compare('hireDate', $this->hireDate, true);
		$criteria->compare('maritalStatus', $this->maritalStatus, true);
		$criteria->compare('currentRank', $this->currentRank, true);
		$criteria->compare('politicalStatus', $this->politicalStatus, true);
		$criteria->compare('contactMobile', $this->contactMobile, true);
		$criteria->compare('contactPhone', $this->contactPhone, true);
		$criteria->compare('originalHomeAddress', $this->originalHomeAddress, true);
		$criteria->compare('currentAddress', $this->currentAddress, true);
		$criteria->compare('emergencyContactName', $this->emergencyContactName, true);
		$criteria->compare('emergencyContactPhone', $this->emergencyContactPhone, true);
		$criteria->compare('identityCardNumber', $this->identityCardNumber, true);
		$criteria->compare('graduationSchool', $this->graduationSchool, true);
		$criteria->compare('certificates', $this->certificates, true);
		$criteria->compare('personalHabits', $this->personalHabits, true);
		$criteria->compare('personalSocialPension', $this->personalSocialPension);
		$criteria->compare('personalSocialInjury', $this->personalSocialInjury);
		$criteria->compare('personalSocialUemployed', $this->personalSocialUemployed);
		$criteria->compare('personalSocialIllness', $this->personalSocialIllness);
		$criteria->compare('personalSocialInsurance', $this->personalSocialInsurance);
		$criteria->compare('familyMembers', $this->familyMembers, true);
		$criteria->compare('educationDetails', $this->educationDetails, true);
		$criteria->compare('workExperience', $this->workExperience, true);
		$criteria->compare('outstandingAchievement', $this->outstandingAchievement, true);
		$criteria->compare('note', $this->note, true);
		$criteria->compare('trialDate', $this->trialDate, true);
		$criteria->compare('trialSalary', $this->trialSalary, true);
		$criteria->compare('employedDate', $this->employedDate, true);
		$criteria->compare('unemployedDate', $this->unemployedDate, true);
		$criteria->compare('signedCommitment', $this->signedCommitment);
		$criteria->compare('signedSecrecyAgreement', $this->signedSecrecyAgreement);
		$criteria->compare('signedTrailContract', $this->signedTrailContract);
		$criteria->compare('signedEmploymentContract', $this->signedEmploymentContract);
		$criteria->compare('signedGuaranteeStatement', $this->signedGuaranteeStatement);
		$criteria->compare('signedJobStatement', $this->signedJobStatement);
		$criteria->compare('companyNote', $this->companyNote, true);
		$criteria->compare('personalPhotoFile', $this->personalPhotoFile, true);
		$criteria->compare('createTime', $this->createTime, true);
		$criteria->compare('createUserId', $this->createUserId);
		$criteria->compare('updateTime', $this->updateTime, true);
		$criteria->compare('updateUserId', $this->updateUserId);

		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}

	public function getFullName() {
		return $this->familyName . $this->givenName;
	}
	
	public function getStatusText(){
		$status=self::getAllStatus();
		return $status[$this->status];
	}	
	
	public function getCurrentProjectName() {
		if($this->currentProjectId>0){
			return $this->currentProject->name;
		}
		return '';
	}
	
}
